﻿namespace EyeOpen.Log4NetExtensions.Appenders
{
    using System.IO;
    using log4net.Appender;

    public class CustomFileAppender : FileAppender
    {
        private readonly string logDirectory;

        public CustomFileAppender(string logDirectory)
        {
            this.logDirectory = logDirectory;
        }

        public string CustomFile
        {
            get;
            set;
        }

        public override void ActivateOptions()
        {
            var fileName = CustomFile;

            var fullName = fileName;

            if (!fullName.Contains("\\"))
            {
                if (logDirectory != null)
                {
                    fullName = Path.Combine(logDirectory, fullName);
                }
            }

            var fullPath = new FileInfo(fullName);

            if (!fullPath.Directory.Exists)
            {
                fullPath
                    .Directory
                    .Create();
            }

            File = fullPath.FullName;

            base.ActivateOptions();
        }
    }
}